package com.hitech;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.alibaba.fastjson.JSONObject;

@SpringBootTest(classes= {Application.class},webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
public class Publisher {
	@Autowired
	private RabbitTemplate rabbitTemplate;

	@Before
	public void init() {
		rabbitTemplate.setConfirmCallback((correlationData,ack,cause)->{
			System.out.println("ack: "+ack+". correlationData: "+correlationData+". cause: "+cause);
		});
	}

	@Test
	public void test() {
		Map<String,Object> msgs = new HashMap<>();
		AtomicInteger c = new AtomicInteger();
		Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(()->{
			for (int i = 0; i< 1; i++) {
				Map<String,Object> msg = new HashMap<>();
				msg.put("routingKey", "key_name");
				Map<String,Object> msgContent = new HashMap<>();
				msgContent.put("loginName", "acc"+c.getAndIncrement());
				msg.put("msgContent", msgContent);
				rabbitTemplate.convertAndSend("exchange_name","#.key_name",JSONObject.toJSONString(msg),new CorrelationData(c.get()+""));
			}
		}, 1, 5, TimeUnit.SECONDS);
	}
}
